using System;
using System.Collections.Generic;
using System.Text;
using Directory.Model;

namespace Directory.BLL
{
    public class Role
    {
        private static Directory.IDAL.IRole dal = new Directory.DAL.Role();

		public RoleInfo CreateRole(string rolename, out RoleInfo.ErrorCode ErrorCode)
		{
			if (BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				return this.CreateRole(rolename, rolename, out ErrorCode);
			}
			else
			{
				ErrorCode = RoleInfo.ErrorCode.InvalidName;
				return null;
			}
		}

		public RoleInfo CreateRole(string rolename, string description, out RoleInfo.ErrorCode ErrorCode)
        {
			if (BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				return dal.CreateRole(rolename, description, out ErrorCode);
			}
			else
			{
				ErrorCode = RoleInfo.ErrorCode.InvalidName;
				return null;
			}
        }

        public bool DeleteRole(string rolename, bool deleteOnlyIfRoleIsEmpty)
        {
			if (BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				RoleInfo.ErrorCode code;
				dal.DeleteRole(rolename.Trim(), deleteOnlyIfRoleIsEmpty, out code);
				return (code == RoleInfo.ErrorCode.Success);
			}
			else
			{
				return false;
			}
        }

		public string[] GetUsersInRole(string rolename, int pageIndex, int pageSize, out int totalRecords)
		{
			totalRecords = 0;
			if (BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				return dal.GetUsersInRole(rolename.Trim(), pageIndex, pageSize, out totalRecords);
			}
			else
			{
				return null;
			}
		}

		public string[] GetRolesOfUser(string username, int pageIndex, int pageSize, out int totalRecords)
		{
			totalRecords = 0;
			if (BLLHelper.ValidateUserName(ref username))
			{
				return dal.GetRolesOfUser(username, pageIndex, pageSize, out totalRecords);
			}
			else
			{
				return null;
			}
		}

        public string[] GetAllRoles(int pageIndex,int pageSize,out int totalRecords)
        {
            return dal.GetAllRoles(pageIndex,pageSize,out totalRecords);
        }

        public void AddUserToRole(string username, string rolename)
        {
			if (BLLHelper.ValidateUserName(ref username) && BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				dal.AddUsersToRoles(username, rolename);
			}
			else
			{
				throw new ArgumentException("Invalid username parameter or rolename parameter.");
			}
        }

        public void RemoveUserFromRole(string username, string rolename)
        {
			if (BLLHelper.ValidateUserName(ref username) && BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				dal.RemoveUsersFromRoles(username, rolename);
			}
			else
			{
				throw new ArgumentException("Invalid username parameter or rolename parameter.");
			}
        }

		public bool IsUserInRole(string username, string rolename)
		{
			if (BLLHelper.ValidateUserName(ref username) && BLLHelper.ValidateNotEmptyStringParameter(ref rolename))
			{
				return dal.IsUserInRole(username, rolename);
			}
			else
			{
				return false;
			}
		}
    }
}
